package com.gsj.maintain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gsj.common.pojo.R;
import com.gsj.common.utils.JwtUtils;
import com.gsj.maintain.mapper.MtShopMapper;
import com.gsj.maintain.pojo.entity.TabMtShop;
import com.gsj.maintain.pojo.vo.MtRecordVo;
import com.gsj.maintain.pojo.vo.MtShopVo;
import com.gsj.maintain.service.MtShopService;
import com.gsj.maintain.utils.SystemTimeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: clms-nine
 * @description: 维修厂业务层
 * @author: 郭书江
 * @create: 2021-07-23 18:57
 **/
@Service
public class MtShopServiceImpl implements MtShopService {
    @Autowired
    private MtShopMapper shopMapper;

    //查询维修厂列表
    @Override
    public R findMtShopByPage(Integer currentPage, Integer pageSize, MtShopVo mtShopVo) {
        //分页
        Page<TabMtShop> page = new Page<>(currentPage,pageSize);
        QueryWrapper<TabMtShop> wrapper = new QueryWrapper<>();

        //模糊查询
        if (StringUtils.isNotEmpty(mtShopVo.getShopName())){
            wrapper.like("name",mtShopVo.getShopName());
        }

        if (StringUtils.isNotEmpty(mtShopVo.getStartDate())) {
            wrapper.ge("addtime",mtShopVo.getStartDate());
        }
        Page<TabMtShop> page1 = shopMapper.selectPage(page, wrapper);

        return R.ok().put("records", page1.getRecords()).put("total", page1.getTotal());


    }

    //添加维修厂
    @Override
    public R insert(TabMtShop mtShop,String username) {
        String time = SystemTimeUtil.getTime();
        mtShop.setOperationTime(time);
        mtShop.setOperator(username);

        int i = shopMapper.insert(mtShop);

        if (i>0){
            return R.ok().put("message","成功");
        }
        return R.error().put("message","失败");
    }

    //更改状态
    @Override
    public R upStatus(Integer id, Integer status) {
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id",id);
        wrapper.set("status", status);
        int i = shopMapper.update(null, wrapper);
        if (i>0){
            return R.ok().put("message","成功");
        }
        return R.error().put("message","失败");
    }
//删除
    @Override
    public R del(Integer id) {
        int i = shopMapper.deleteById(id);
        if (i>0){
            return R.ok().put("message","成功");
        }
        return R.error().put("message","失败");
    }

    //修改
    @Override
    public R up(TabMtShop mtShop) {
        UpdateWrapper<TabMtShop> wrapper = new UpdateWrapper<>();
        String time = SystemTimeUtil.getTime();
        mtShop.setOperationTime(time);
        wrapper.set("name",mtShop.getName());
        wrapper.set("address",mtShop.getAddress());
        wrapper.set("phone_num",mtShop.getPhoneNum());
        wrapper.set("addtime",mtShop.getAddtime());
        wrapper.set("remark",mtShop.getRemark());
        wrapper.set("operation_time", mtShop.getOperationTime());
        wrapper.eq("id", mtShop.getId());
        int i = shopMapper.update(null, wrapper);
        if (i>0){
            return R.ok().put("message","修改成功");
        }
        return R.error().put("message","修改失败");
    }
//修改时根据回显数据
    @Override
    public R selById(Integer id) {
        TabMtShop tabMtShop = shopMapper.selectById(id);
        return R.ok().put("MtShop",tabMtShop);
    }

    //校验
    @Override
    public R selMtShopName(TabMtShop mtShop) {
        List<TabMtShop> tabMtShops = shopMapper.selectList(null);
        int result=0;
        for (TabMtShop shop : tabMtShops) {
            if (mtShop.getId()!=null){
                if (!mtShop.getId().equals(shop.getId())){
                    if (mtShop.getName().equals(shop.getName())){
                        result=1;
                        break;
                    }
                }
            }else {
                if (mtShop.getName().equals(shop.getName())){
                    result=1;
                    break;
                }
            }
        }
        return R.ok().put("code",result);
    }
    //查询维修厂
    @Override
    public R selectListAll() {
        QueryWrapper<TabMtShop> wrapper = new QueryWrapper<>();
        wrapper.eq("status","0");
        List<TabMtShop> mtShops = shopMapper.selectList(wrapper);
        return R.ok().put("mtShops",mtShops);
    }

}
